package handler

import (
	"html/template"
	"net/http"
	"runtime"

	"chainmaker.org/chainmaker/smarteditor/common"
	"chainmaker.org/chainmaker/smarteditor/conf"
	"chainmaker.org/chainmaker/smarteditor/i18n"
	"chainmaker.org/chainmaker/smarteditor/logger"
	"chainmaker.org/chainmaker/smarteditor/session"
	"github.com/88250/gulu"
)

var log = logger.Get()

func aboutHandler(w http.ResponseWriter, r *http.Request) {
	uid, err := session.GetUidFromSession(r)
	if err != nil {
		http.Error(w, common.Forbidden, http.StatusForbidden)
		return
	}

	t, err := template.ParseFiles(common.AboutPage)
	if err != nil {
		log.Errorf("[aboutHandler] template parse file fail. err:%v", err)
		http.Error(w, err.Error(), http.StatusInternalServerError)
		return
	}

	err = t.Execute(w, getAboutPageModule(uid))
	if err != nil {
		log.Errorf("[aboutHandler] render template fail. err:%v", err)
		http.Error(w, err.Error(), http.StatusInternalServerError)
		return
	}
}

func getAboutPageModule(uid string) map[string]interface{} {
	locale := conf.GetUser(uid).Locale
	return map[string]interface{}{
		"conf":   conf.Wide,
		"i18n":   i18n.GetAll(locale),
		"locale": locale,
		"ver":    conf.WideVersion,
		"goos":   runtime.GOOS,
		"goarch": runtime.GOARCH,
		"gover":  runtime.Version(),
	}
}

func getLoginURLS(w http.ResponseWriter, r *http.Request) {
	gulu.Ret.RetResult(w, r, &gulu.Result{Code: 0, Msg: "", Data: conf.Wide.VersionUrlS})
}
